//
//  ESFacialTypeView.m
//  SportsGather
//
//  Created by fuyoufang on 15/10/18.
//  Copyright (c) 2015年 Egymcom. All rights reserved.
//

#import "ESFacialTypeView.h"
#define ButtonWith 60

@interface ESFacialTypeView ()
/** 发送按钮 */
@property (nonatomic,weak) UIButton *sendButton;
@property (nonatomic,weak) UIScrollView *typesScrollView;
/** 选中的表情类型按钮 */
@property (nonatomic,weak) UIButton *selectedTypeButton;
@end

@implementation ESFacialTypeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    UIButton *sendButton = [[UIButton alloc] init];
    sendButton.translatesAutoresizingMaskIntoConstraints = NO;
    sendButton.backgroundColor = [UIColor orangeColor];
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(sendButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sendButton];
    self.sendButton = sendButton;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = NO;
    scrollView.bounces = YES;
    scrollView.userInteractionEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:scrollView];
    self.typesScrollView = scrollView;
    
    NSDictionary *subviews = NSDictionaryOfVariableBindings(sendButton,scrollView);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:subviews]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(4)-[sendButton]-(4)-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:subviews]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]-(4)-[sendButton(ButtonWith)]-(4)-|" options:NSLayoutFormatAlignAllCenterY metrics:@{@"ButtonWith": @(ButtonWith)} views:subviews]];
}

- (void)setFacialTypes:(NSArray *)facialTypes {
    _facialTypes = facialTypes;
    if (facialTypes.count > 0) {
        for (int i = 0; i < facialTypes.count; i++) {
            UIButton *button = [[UIButton alloc] init];
            button.translatesAutoresizingMaskIntoConstraints = NO;
            [button setTitle:facialTypes[i] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(facialTypesClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.typesScrollView addSubview:button];
            [self.typesScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]-(0)-|" options: NSLayoutFormatAlignAllBottom metrics:nil views:@{@"button" : button}]];
            [self.typesScrollView addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.typesScrollView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:(i * ButtonWith)]];
            [button addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:ButtonWith]];
            
        }
        // 修改 contentSize 一定放在 dispatch_async(dispatch_get_main_queue(), ^{});
        dispatch_async(dispatch_get_main_queue(), ^{
            self.typesScrollView.contentSize = CGSizeMake(ButtonWith * self.facialTypes.count, 0);
        });
        
    }
}

/**
 *  发送按钮点击
 */
- (void)sendButtonClick {
    if ([self.delegate respondsToSelector:@selector(facialTypeViewDidSelectedSend:)]) {
        [self.delegate facialTypeViewDidSelectedSend:self];
    }
}

/**
 *  表情类型点击
 */
- (void)facialTypesClick:(UIButton *)sender {
    if (self.selectedTypeButton == sender) {
        return;
    }
    self.selectedTypeButton.selected = NO;
    self.selectedTypeButton = sender;
    if ([self.delegate respondsToSelector:@selector(facialTypeView:didSelectedType:)]) {
        [self.delegate facialTypeView:self didSelectedType:sender.titleLabel.text];
    }
}

@end
